<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/1/12
 * Time: 15:03
 */
class Courseo2o_model extends CI_Model
{
  /**
   * 删除
   * @param  [type] $course_id [description]
   * @return [type]            [description]
   */
  public function delete($course_id)
  {
    //开始事务
    $this->db->trans_start();
    $this->db->delete('course',array('id' => $course_id));

    $this->db->delete('course_subject',array('course_id' => $course_id));

    $this->db->trans_complete();
    return 1;
  }
  /**
  * 发布班课
  */
  public function publish_course_by_school($user_id,$teacher_id,$school_id,$data)
  {

    //开始事务
    $this->db->trans_start();
    $course = array(
      'uid' => $user_id,
      'school_id' => $school_id,
      'teacher_id' => $teacher_id,
      'cate_id' => $data['cate_id'],
      'subject_id' => 0,
      'type' => 3,
      'title' => $data['title'],
      'course_img' => $data['course_imgs'][0],
      'course_imgs' => json_encode($data['course_imgs']),
      'detail' => $data['detail'],
      'price' => $data['price'],
      'course_time' => $data['course_time'],
      'class_number' => $data['class_number'],
      'effective_time' => 0,
      'suitable' => $data['suitable'],
      'learn_what' => $data['learn_what'],
      'click_num' => 0,
      'status' => 1,
      'sales' => 0,
      'is_recomend' => 0,
      'star1' => 0,
      'star2' => 0,
      'star3' => 0,
      'star4' => 0,
      'star5' => 0,
      'comment_count' => 0,
      'add_time' => time(),
    );
    //插入课程表
    $this->db->insert('course',$course);
    $course_id = $this->db->insert_id();
    //插入课程科目表
    foreach ($data['subject'] as $sub) {

      $subject1 = array(
        'course_id' => $course_id,
        'subject_id' => $sub,
        'create_on' => time()
      );

      $this->db->insert('course_subject',$subject1);
    }


    $this->db->trans_complete();
    return 1;
  }

  /**
  * 修改班课
  */
  public function edit_course_by_school($course_id,$data)
  {
    // if(empty($data['img_url'])) return -1;

    $this->db->trans_start();
    $course = array(
      'title' => $data['title'],
      'course_img' => $data['course_imgs'][0],
      'course_imgs' => json_encode($data['course_imgs']),
      'detail' => $data['detail'],
      'price' => $data['price'],
      'cate_id' => $data['cate_id'],
      'course_time' => $data['course_time'],
      'class_number' => $data['class_number'],
      'suitable' => $data['suitable'],
      'learn_what' => $data['learn_what'],
      'is_recomend' => $data['is_recomend']
    );
    $this->db->update('course',$course, array('id' => $course_id ));
    //删除课程科目
    $sql = "delete from {$this->db->dbprefix}course_subject where course_id = {$course_id}";
    $this->db->query($sql);

    //插入课程科目表
    foreach ($data['subject'] as $sub) {
      $subject1 = array(
        'course_id' => $course_id,
        'subject_id' => $sub,
        'create_on' => time()
      );

      $this->db->insert('course_subject',$subject1);
    }


    $this->db->trans_complete();

    return 1;
  }

  /**
  * 获取学校班课（分页）
  */
  public function get_course_by_school($limit = 0, $offset = 10, $conditions = array())
  {
    $where = 'type = 3 ';
    if(!empty($conditions['school_id'])){
        $where .= " AND school_id = '{$conditions['school_id']}' ";
    }
    if(!empty($conditions['name'])){
        $where .= " AND title LIKE '%{$conditions['name']}%'";
    }
    if($conditions['status'] != 10){
        $where .= " AND status = {$conditions['status']}";
    }
    if(!empty($conditions['cate_id'])){
        $where .= " AND cate_id = {$conditions['cate_id']}";
    }
    if(!empty($conditions['kemu'])){
        $where .= " AND subject_id = {$conditions['kemu']}";
    }

    if($this->is_manage != 1){
        $where .= " AND teacher_id = '{$this->user_data['teacher_id']}' ";
    }

    $this->db->trans_start();
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}course where $where order by id desc LIMIT $limit, $offset;";

    $result = $this->db->query($sql)->result_array();
    $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();

    $this->db->trans_complete();

    return array("total_count" => $row_num['row_num'], "result" => $result);
  }

  /**
  **  获取学校所有课程
  */
  public function get_course_all_by_school($school_id,$course_type,$status= -1)
  {
    $where = "";
    // if(!empty($course_type)) $where .= " AND c.type = {$course_type}";
    // if($status>-1) $where .= " AND c.status = {$status}";
    $where = " (s.school_id = '{$school_id}' AND s.status=1) ";
    $sql = "SELECT  c.*  FROM {$this->db->dbprefix}course AS c ";
    $sql .= "LEFT JOIN {$this->db->dbprefix}course_shelves AS s ON(s.course_id=c.id) ";
    $sql .= "where $where GROUP BY c.id ";
    return $this->db->query($sql)->result_array();
  }

  /**
  ** 获取班课详情
  */
  function get_course_by_id($id){
    $sql = "SELECT * FROM {$this->db->dbprefix}course where id = {$id} ";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 获取面授课关联的科目list
   *
   * @param  [type] $course_id [description]
   * @return [type]            [description]
   */
  public function get_course_subject_list($course_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}course_subject where course_id = {$course_id} ";
    return $this->db->query($sql)->result_array();
  }

  public function get_offline_course_by_id($id){
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_course where id = {$id}";
    return $this->db->query($sql)->row_array();
  }

  function delete_offlice_course($id){
    return $this->db->delete('o2o_course', array('id' => $id ));
  }

  function delete_offlice_cate($id){

    return $this->db->delete('course_cate', array('id' => $id ));
  }

  /**
   * 获取线下课程
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function get_offline_course($school_id,$cate_id=-1)
  {
    $where = " c.school_id = {$school_id} ";
    if($cate_id>-1){
      $where .= "and c.cate_id = {$cate_id}";
    }
    $sql = "SELECT c.*,ca.name as cate_name FROM {$this->db->dbprefix}o2o_course c left join {$this->db->dbprefix}course_cate ca on ca.id = c.cate_id where {$where}  order by c.id desc ";
    // $sql = "SELECT * FROM {$this->db->dbprefix}o2o_course where school_id = {$school_id} order by cate_id asc, id asc";
    return $this->db->query($sql)->result_array();
  }

  /**
   * 根据课程分类获取线下课程
   * @param  [type] $cate_id [description]
   * @return [type]          [description]
   */
  public function get_offline_course_by_cate($cate_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_course c where cate_id = {$cate_id} ";
    return $this->db->query($sql)->result_array();
  }

  /**
   * 添加线下课程
   * @param [type] $data      [description]
   * @param [type] $school_id [description]
   */
  public function offline_course_add($data,$school_id)
  {
    $course = array(
      'cate_id' => $data['cate_id'],
      'subject_id' => 0,
      'school_id' => $school_id,
      'title' => $data['title'],
      'course_img' => '',
      'unit' => $data['unit'],
      'price' => $data['price'],
      'detail' => '',
      'one_class' => $data['one_class'],
      'class_time' => $data['class_time'],
      'status' => 1,
      'add_time' => time(),
    );
   return $this->db->insert('o2o_course',$course);
  }

  public function offline_course_edit($data){
    $course = array(
      'cate_id' => $data['cate_id'],
      'subject_id' => 0,
      'title' => $data['title'],
      'unit' => $data['unit'],
      'price' => $data['price'],
      'one_class' => $data['one_class'],
      'class_time' => $data['class_time'],
    );
   return $this->db->update('o2o_course',$course,  array('id' => $data['id'] ));
  }

  /**
   * 获取线下课程分类
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function get_offline_cate($school_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}course_cate where school_id = {$school_id} and type = 2 order by sort desc, id desc ";
    return $this->db->query($sql)->result_array();
  }

  function get_offline_cate_by_id($id){
    $sql = "SELECT * FROM {$this->db->dbprefix}course_cate where id = {$id} ";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 添加线下课程分类
   * @param [type] $name      [description]
   * @param [type] $sort      [description]
   * @param [type] $school_id [description]
   */
  public function offline_cate_add($name,$sort,$school_id)
  {
    $cate = array(
      'pid' => 0,
      'school_id' => $school_id,
      'type' => 2,
      'name' => $name,
      'sign' => '',
      'content' => '',
      'sort' => $sort,
      'status' => 1,
      'add_time' => time(),
      'level' => 1,
      'v_count' => 0,
    );
    return $this->db->insert('course_cate',$cate);
  }

  function offline_cate_edit($name,$sort,$id){
    $cate = array(
      'name' => $name,
      'sort' => $sort,
    );
    return $this->db->update('course_cate',$cate, array('id' => $id ));
  }

  function get_class_course($limit = 0, $offset = 10, $conditions = array()){
    $where = "WHERE school_id =  '{$this->session->userdata['user_data']['school_id']}' AND teacher_id = '{$this->session->userdata['user_data']['teacher_id']}'";
    if(!empty($conditions['class_id'])){
      $where .= " AND class_id = '{$conditions['class_id']}' ";
    }
    if(!empty($conditions['name'])){
      $where .= " AND name LIKE '%{$conditions['name']}%' ";
    }
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}o2o_class_courses $where order by id desc LIMIT $limit, $offset";
    $result = $this->db->query($sql)->result_array();
    $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
    return array("total_count" => $row_num['row_num'], "result" => $result);
  }
}
